package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Bidding;

import com.demo.villageresidence.service.BiddingService;
import com.demo.villageresidence.service.TenderService;
import com.demo.villageresidence.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/bidding")
public class BiddingController {

    @Autowired
    private BiddingService biddingService;

    @Autowired
    private TenderService tenderService;

    @Autowired
    private UserService userService;

    /**
     * 新增投标
     * @param bidding
     * @return
     */
    @PostMapping("addBidding")
    public Result addBidding(@RequestBody Bidding bidding){

        bidding.setBiddingTime(LocalDateTime.now());
        bidding.setIsDelete(0);
        // 设置初始值为待处理
        bidding.setBiddingStatus(2);
        if (biddingService.save(bidding)){
            return Result.ok("投标成功");
        }
        return Result.fail("投标失败");
    }

    @GetMapping("getBiddingByTenderIdPage")
    public Result getBiddingByTenderIdPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "tenderId") Integer tenderId){

        Page<Bidding> page = biddingService.query().eq("tender_id", tenderId)
                .page(new Page<>(current, pageSize));

        List<Bidding> records = page.getRecords();

        for (Bidding bidding : records) {
            bidding.setUser(userService.getById(bidding.getUserId()));
        }

        return Result.ok(records);
    }

    @GetMapping("getBiddingByUserId/{userId}")
    public Result getBiddingByUserId(@PathVariable(value = "userId") Integer userId){
        List<Bidding> biddingList = biddingService.query().eq("user_id", userId).list();
        biddingList.forEach(bidding -> bidding.setTender(tenderService.getById(bidding.getTenderId())));
        return Result.ok(biddingList);
    }

    @PutMapping("updateWinBidding")
    public Result updateWinBidding(@RequestBody List<Integer> biddingIds){
        List<Bidding> biddingList = biddingService.listByIds(biddingIds);
        biddingList.forEach(bidding -> bidding.setBiddingStatus(1));
        if (biddingService.updateBatchById(biddingList)){
            return Result.ok("中标成功");
        }
        return Result.fail("参数错误");
    }

    @PutMapping("updateFailBidding")
    public Result updateFailBidding(@RequestBody List<Integer> biddingIds){
        System.out.println("biddingIds = " + biddingIds);

        List<Bidding> biddingList = biddingService.listByIds(biddingIds);
        biddingList.forEach(bidding -> bidding.setBiddingStatus(0));
        if (biddingService.updateBatchById(biddingList)){
            return Result.ok("已落选");
        }
        return Result.fail("参数错误");
    }

    @GetMapping("isAddBiddingByUserIdAndTenderId")
    public Result isAddBiddingByUserIdAndTenderId(@RequestParam(value = "userId") Integer userId,
                                                  @RequestParam(value = "tenderId") Integer tenderId){
        Bidding bidding = biddingService.query().eq("user_id", userId).eq("tender_id", tenderId).one();
        if (bidding != null){
            return Result.fail("已投标");
        }
        return Result.ok("未投标");
    }

    @GetMapping("getBiddingNum")
    public Result getBiddingNum(){
        Integer biddingStatus = biddingService.query().eq("bidding_status", 2).count();
        if (biddingStatus>0) {
            return Result.ok(biddingStatus);
        }else {
            return Result.ok(0);
        }

    }







}
